package com.yunwaikeji.gy_tool_library.widget.recycler;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yunwaikeji.gy_tool_library.info.GYRecyclerInfo;
import com.yunwaikeji.gy_tool_library.interface1.GYViewBgInterface;
import com.yunwaikeji.gy_tool_library.interface1.GYViewGroupInterface;
import com.yunwaikeji.gy_tool_library.set.GYViewBgBuilder;
import com.yunwaikeji.gy_tool_library.set.GYViewGroupSetSet;

public class GYRecyclerView extends RecyclerView implements GYViewBgInterface<GYRecyclerView, GYRecyclerView.GYViewBgBuilder1>, GYViewGroupInterface<GYRecyclerView, GYRecyclerView.GYViewSet>, GYRecyclerInfo {
    public GYRecyclerView(@NonNull Context context) {
        super(context);
        initView(null);
    }

    public GYRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(attrs);
    }

    public GYRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(attrs);
    }

    @Override
    public void initView(AttributeSet attributeSet) {
        getBackgroundBuilder(attributeSet).build();
        getRecyclerBuilder(attributeSet).build();
        getViewSet().setOverScrollMode(GYRecyclerBuilder.getOverScrollMode());
    }

    @Override
    public GYViewBgBuilder1 setBackgroundBuilder(GYViewBgBuilder1 gyViewBgBuilder1) {
        viewBgBuilder1 = gyViewBgBuilder1;
        return viewBgBuilder1;
    }

    private GYViewBgBuilder1 viewBgBuilder1;

    @Override
    public GYViewBgBuilder1 getBackgroundBuilder() {
        return getBackgroundBuilder(null);
    }

    @Override
    public GYViewBgBuilder1 getBackgroundBuilder(AttributeSet attributeSet) {
        if (viewBgBuilder1 == null) {
            viewBgBuilder1 = new GYViewBgBuilder1(this, attributeSet);
        }
        return viewBgBuilder1;
    }

    private GYViewSet viewSet;

    @Override
    public GYViewSet getViewSet() {
        if (viewSet == null) {
            viewSet = new GYViewSet(this);
        }
        return viewSet;
    }

    public static class GYViewBgBuilder1 extends GYViewBgBuilder<GYRecyclerView, GYViewBgBuilder1> {
        public GYViewBgBuilder1(View view, AttributeSet attributeSet) {
            super(view, attributeSet);
        }

        public GYViewBgBuilder1(View view) {
            super(view);
        }
    }

    public class GYViewSet extends GYViewGroupSetSet<GYRecyclerView, GYViewSet> {
        public GYViewSet(View view) {
            super(view);
        }

        //    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        this.requestDisallowInterceptTouchEvent(true); //事件不传递给父布局
//        return super.dispatchTouchEvent(ev);
//    }

        public GYViewSet setPagerPosition(int position) {
            LayoutManager layoutManager = GYRecyclerView.this.getLayoutManager();
            layoutManager.scrollToPosition(position);
            return this;
        }

        public GYViewSet setNestedScrollingEnabled(boolean enabled) {
            GYRecyclerView.super.setNestedScrollingEnabled(enabled);
            return this;
        }

        public GYViewSet setScroll(boolean scroll) {
            GYRecyclerView.super.setNestedScrollingEnabled(scroll);
            return this;
        }

        public boolean isScroll() {
            return GYRecyclerView.super.isNestedScrollingEnabled();
        }

        public int getPagerPosition() {
            LayoutManager layoutManager = GYRecyclerView.this.getLayoutManager();
            return ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
        }

        public GYViewSet setItemAnimator(ItemAnimator animator) {
            GYRecyclerView.super.setItemAnimator(animator);
            return this;
        }

        public GYViewSet setOverScrollMode(int overScrollMode) {
            GYRecyclerView.super.setOverScrollMode(overScrollMode);
            return this;
        }
    }

    private GYRecyclerBuilder GYRecyclerBuilder;

    public GYRecyclerBuilder getRecyclerBuilder() {
        return getRecyclerBuilder(null);
    }

    public GYRecyclerBuilder getRecyclerBuilder(AttributeSet attributeSet) {
        if (GYRecyclerBuilder == null) {
            GYRecyclerBuilder = new GYRecyclerBuilder(this, attributeSet);
        }
        return GYRecyclerBuilder;
    }

    private OnPagerChangeListener onPagerChangeListener;

    public OnPagerChangeListener getOnPagerChangeListener() {
        return onPagerChangeListener;
    }

    public interface OnPagerChangeListener {
        void onPagerChange(int position);
    }

    public GYRecyclerView setOnPagerChangeListener(OnPagerChangeListener onPagerChangeListener) {
        this.onPagerChangeListener = onPagerChangeListener;
        return this;
    }
}
